package com.xxg.renrenproduct.service;

import com.alibaba.fastjson.JSON;
import com.xxg.renrencommon.domain.dao.RenrenShopGoods;
import com.xxg.renrencommon.domain.result.BaseResult;
import com.xxg.renrencommon.exception.exceptionenum.ProductExceptionCodeCodeEnum;
import com.xxg.renrenproduct.component.ProductComponent;
import com.xxg.renrenproduct.dao.*;
import com.xxg.renrencommon.domain.bean.ProductGatherInfo;
import com.xxg.renrenproduct.domain.query.GatherProductQuery;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 类说明
 *
 * @author szj
 * @date 2021/11/30
 */
@Service
@Slf4j
@AllArgsConstructor
public class ProductGatherServiceImpl implements ProductGatherService{
    private final RenrenShopGoodsDao renrenShopGoodsDao;
    private final ProductGatherInfoDao productGatherInfoDao;
    private final RenrenShopShopDao renrenShopShopDao;
    private final ProductComponent productComponent;
    private final static ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), (r) -> {
        Thread thread = new Thread(r);
            thread.setName("ProductGather_");
            return thread;
    }, new ThreadPoolExecutor.CallerRunsPolicy());

    @Override
    public BaseResult<Void> gather(String userId, GatherProductQuery query) {
        EXECUTOR.execute(() -> {
            Integer userIdInt = Integer.parseInt(userId);
            Integer spuId = query.getSpuId();
            ProductGatherInfo productGatherInfo = productGatherInfoDao.findByUserIdAndSpuId(userIdInt, spuId);
            if (productGatherInfo != null) {
                log.info("gather:{}", ProductExceptionCodeCodeEnum.HAS_GATHER.getName());
                return;
            }
            Integer shopId = renrenShopShopDao.selectNormalShopIdByUserId(userIdInt);
            if (shopId == null) {
                log.info("gather:{}", ProductExceptionCodeCodeEnum.NO_SHOP_UNDER_USER.getName());
                return;
            }
            // 判断该商品是否属于自己
            long count = renrenShopGoodsDao.countByIdAndShopId(spuId, shopId);
            if (count > 0) {
                log.info("gather:{}", ProductExceptionCodeCodeEnum.CAN_NOT_GATHER_SELF.getName());
                return;
            }
            Integer originShopId = renrenShopGoodsDao.selectShopIdByPrimaryKey(query.getSpuId());
            productGatherInfo = new ProductGatherInfo();
            productGatherInfo.setSpuId(spuId);
            productGatherInfo.setOriginShopId(originShopId);
            productGatherInfo.setUserId(userIdInt);
            productGatherInfo.setShopId(shopId);
            try {
                productGatherInfo = productComponent.copyGoodsToCurrentUserShop(userIdInt, shopId, spuId, query.getCategoryIds(), null, productGatherInfo);
            } catch (Exception e) {
                log.error("gather_" + ProductExceptionCodeCodeEnum.GATHER_FAIL.getName() + "_" + e.getMessage(), e);
                return;
            }
            log.info("ready insert productGatherInfo:{}", JSON.toJSONString(productGatherInfo));
            productGatherInfoDao.insert(productGatherInfo);
        });

        return new BaseResult<>();
    }

}
